package com.fast.controller;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fast.common.web.AbstractController;
import com.fast.common.web.Response;
import com.fast.modules.acd.entity.ClientJoin;
import com.fast.modules.acd.entity.Node;
import com.fast.modules.acd.exception.SkillNotSupportException;
import com.fast.modules.acd.service.AssignService;
import com.fast.modules.acd.service.QueueService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;

/**
 * 面向客户端暴露的controller
 * @author yaoyuquan
 *
 */
@RestController
@RequestMapping("/client")
@Api(tags = "面向客户服务的接口 controller")
public class ClientController extends AbstractController {
	
	
	@Autowired
	QueueService queueService;
	
	@Autowired
	AssignService assignService;	
	
	
	/**
	 * 获取当前排队的队列状态
	 * @return
	 */
	@GetMapping("/status")
	@ApiOperation(hidden=true, value = "队列状态的接口")
    @ApiResponse(code = 200, message = "返回调用结果的包装对象")
	public Response status() {
		return success(queueService.status());
	}
	
	
	/**
	 * 将用户请求加入队列
	 * @param node
	 * @return
	 */
	@PostMapping(value="/join", consumes="application/x-www-form-urlencoded")
	@ApiOperation(value="用户加入排队队列", notes="用户转人工交互，加入排队队列", 
    tags= {"tag1","tag2"}, response=Response.class, produces="application/json")
    @ApiResponse(code = 200, message = "返回的payload表示当前队列的排队人数，当排队人数为0时进入分配程序，分配成功后可以和客服交互")
	@ApiParam(name="用户技能ID", value="用户技能ID", required=true, example="1")
	public Response join(ClientJoin client) {
		
		Response result;
		
		int size = 0;
		
		try {
			Node node = new Node(client.getClientID(), client.getSkillID());
			
			
			size = queueService.join(node);
			result = success(size);
		} catch (IllegalArgumentException e) {
			result = fail(e.getMessage());
		}
		
		return result;
	}
	
	
	
	/**
	 * 用户取消排队
	 * @param node
	 * @return
	 */
	@PostMapping("/cancel")
	@ApiOperation(value="用户取消排队", notes="用户取消排队，或者断线，将排队数据从队列中取出，并通知后面的用户", 
    tags= {"tag1","tag2"}, response=Response.class, produces="application/json")
    @ApiResponse(code = 200, message = "返回调用结果的包装对象")
	@ApiParam(name="技能ID", value="用户技能ID", required=true, example="1") 
	public Response cancel(@RequestBody String clientID) {
		
		queueService.cancel(clientID);

		return success();
	}
	
	
	
	/**
	 * 测试的方法， 运行一次分配的方法
	 * 实际线上环境中会用定时任务执行
	 * @param node
	 * @return
	 * @throws SkillNotSupportException 
	 * @throws IOException 
	 */
	@ApiOperation(hidden=true, value = "分配一个用户请求")
	@GetMapping("/run")
	public String run() throws SkillNotSupportException, IOException {
		
		assignService.assign();
		
		return "OK";
	}

}
